package com.legal.app.common;

/*
 * @Description AjacxResult
 * @Author Ale
 * @Date 2025/6/14 16:48
 */

import java.util.HashMap;

public class AjaxResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    // 状态码
    public static final String CODE_TAG = "code";
    // 返回消息
    public static final String MSG_TAG = "msg";
    // 返回数据
    public static final String DATA_TAG = "data";

    // 状态类型
    public enum Type {
        SUCCESS(200),
        ERROR(500),
        WARN(400),
        UNAUTHORIZED(401),
        FORBIDDEN(403);

        private final int value;
        Type(int value) { this.value = value; }
        public int value() { return this.value; }
    }

    // 构造方法
    public AjaxResult() {}

    public AjaxResult(Type type, String msg) {
        super.put(CODE_TAG, type.value());
        super.put(MSG_TAG, msg);
    }

    public AjaxResult(Type type, String msg, Object data) {
        super.put(CODE_TAG, type.value());
        super.put(MSG_TAG, msg);
        if (data != null) {
            super.put(DATA_TAG, data);
        }
    }

    // 静态工厂方法
    public static AjaxResult success() { return new AjaxResult(Type.SUCCESS, "操作成功"); }
    public static AjaxResult success(Object data) { return new AjaxResult(Type.SUCCESS, "操作成功", data); }
    public static AjaxResult success(String msg) { return new AjaxResult(Type.SUCCESS, msg); }
    public static AjaxResult success(String msg, Object data) { return new AjaxResult(Type.SUCCESS, msg, data); }

    public static AjaxResult error() { return new AjaxResult(Type.ERROR, "操作失败"); }
    public static AjaxResult error(String msg) { return new AjaxResult(Type.ERROR, msg); }
    public static AjaxResult error(int code, String msg) {
        AjaxResult ajax = new AjaxResult();
        ajax.put(CODE_TAG, code);
        ajax.put(MSG_TAG, msg);
        return ajax;
    }
}
